package cn.itcast.controller;

import cn.itcast.entity.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.CheckItem;
import cn.itcast.service.CheckItemService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    CheckItemService service;


    @RequestMapping("/add.do")
    public Result add(@RequestBody CheckItem item) {

        service.add(item);

        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);

    }

    //将页面的数据封装到QueryPageBean完成后台分页查寻
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/queryPage.do")
    public PageResult queryPage(@RequestBody QueryPageBean pageBean) {

       // PageResult result = service.queryPage(pageBean);

        return service.queryPage(pageBean);

    }


    //根据id查寻检查项信息
    @RequestMapping("/findById.do")
    public CheckItem findById(String id) {

        //   CheckItem item = service.findById(id);

        return service.findById(id);
    }


    //根据id完成修改
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody CheckItem item) {


        try {

            service.edit(item);

        }catch(Exception e){
            e.printStackTrace();

            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }


        return new Result(false,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }



    //查寻所有检查项
    @RequestMapping("/findAll.do")
    public List<CheckItem> findAll() {

        return service.findAll();
    }
}
